//
//  QRCodeCardViewController.swift
//  SinaWeibo
//
//  Created by 胡阳 on 16/5/23.
//  Copyright © 2016年 young4ever. All rights reserved.
//

import UIKit

class QRCodeCardViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        // 1.设置标题
        navigationItem.title = "我的名片"
        
        // 2.添加图片容器
        view.addSubview(imageView)
        imageView.xmg_AlignInner(type: XMG_AlignType.Center, referView: view, size: CGSize(width: 200, height: 200))
        
        // 3.生成二维码
        let img = createQRCodeImage()
        
        // 4.将生成的二维码显示在图片上
        imageView.image = img        
    }
    
    private func createQRCodeImage() -> UIImage {
    
        //1.创建滤镜
        let filter = CIFilter(name: "CIQRCodeGenerator")
        // 注意:必须设置这个基本参数 , 防止之前设置覆盖我们要设置的参数
        filter?.setDefaults()
        filter?.setValue("ALittleNasty".dataUsingEncoding(NSUTF8StringEncoding), forKey: "inputMessage")
        // 拿到滤镜输出的图片
        let  ciImage = filter?.outputImage
        // 根据ciImage生成二维码
        let bgImage = createNonInterpolatedUIImageFormCIImage(ciImage!, size: 300)
        // 返回合成后的图片
        return composeImage(bgImage, iconImage: UIImage(named: "avator.jpg")!)
    }
    
    private func composeImage(bgImage:UIImage, iconImage:UIImage) -> UIImage
    {
        // 开启图片上下文
        UIGraphicsBeginImageContext(bgImage.size)
        // 绘制背景图片
        bgImage.drawInRect(CGRect(origin: CGPointZero, size: bgImage.size))
        
        // 绘制头像
        let width:CGFloat = 50
        let height:CGFloat = width
        let x = (bgImage.size.width - width) * 0.5
        let y = (bgImage.size.height - height) * 0.5
        iconImage.drawInRect(CGRect(x: x, y: y, width: width, height: height))
        
        // 拿到上下文输出的图片
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        
        // 关闭上下文
        UIGraphicsEndImageContext()
        // 返回图片
        return newImage
    }
    
    /**
     根据CIImage生成指定大小的高清UIImage
     
     :param: image 指定CIImage
     :param: size    指定大小
     :returns: 生成好的图片
     */
    private func createNonInterpolatedUIImageFormCIImage(image: CIImage, size: CGFloat) -> UIImage {
        
        let extent: CGRect = CGRectIntegral(image.extent)
        let scale: CGFloat = min(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent))
        
        // 1.创建bitmap;
        let width = CGRectGetWidth(extent) * scale
        let height = CGRectGetHeight(extent) * scale
        let cs: CGColorSpaceRef = CGColorSpaceCreateDeviceGray()!
        let bitmapRef = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, cs, 0)!
        
        let context = CIContext(options: nil)
        let bitmapImage: CGImageRef = context.createCGImage(image, fromRect: extent)
        
        CGContextSetInterpolationQuality(bitmapRef,  CGInterpolationQuality.None)
        CGContextScaleCTM(bitmapRef, scale, scale);
        CGContextDrawImage(bitmapRef, extent, bitmapImage);
        
        // 2.保存bitmap到图片
        let scaledImage: CGImageRef = CGBitmapContextCreateImage(bitmapRef)!
        
        return UIImage(CGImage: scaledImage)
    }
    
    /// 图片
    private lazy var imageView : UIImageView = {
    
        let iv = UIImageView()
        return iv
    }()
}
